<?php
/**
 * User: bim bip
 * Date: 8/4/11
 * Time: 8:08 PM
 */
class Ad_Smile extends CI_Controller{
    var $_imageDir = 'images/smile/';
    var $_message = '';
    function __construct(){
        parent::__construct();
        if(!$this->session->userdata('logged_in')){
            echo "You must refresh your browser to be continute!!!";
            die;
        }
    }

    function index(){
        //load smile list
        $this->load->model('site/smilemodel');
        $smile = new SmileModel();
        $data['smileList'] = $smile->getSmileList();
        $this->load->view('manage/smile/list', $data);
    }

    function addNew(){
        if(!$this->input->post('addnew-submit')){
            $data['site_message'] = $this->_message;
            $this->load->view('manage/smile/new', $data);
        }
        else{
            $img = $this->_doUpload('image-upload');
            if(!$img){ //if error during upload file
                $data['site_message'] = 'Có lỗi trong quá trình upload, xin thử lại!!';
                $this->load->view('manage/smile/new', $data);
            }else{
                //save to database
                $this->load->model('site/smilemodel');
                $smile = new SmileModel();
                $smile->_title = $this->input->post('title');
                $smile->_text = $this->input->post('text');
                $smile->_path = $img;
                //save to database
                $smileID = $smile->insert();

                //redirect to update
                redirect("admincp/smile/update/$smileID", 'location', 302);
            }
        }
    }


    function update($smileID){
        if(!$this->input->post('update-submit')){
            //load info
            $this->load->model('site/smilemodel');
            $smile = new SmileModel();
            $smile = $smile->getSmile($smileID);
            $data['title'] = $smile['title'];
            $data['text'] = $smile['text'];
            $data['path'] = $smile['path'];
            //load view
            $this->load->view('manage/smile/update', $data);
        }else{
            //select smile info
            $this->load->model('site/smilemodel');
            $smile = new SmileModel();
            $smile->_title = $this->input->post('title');
            $smile->_text = $this->input->post('text');
            $smile->_id = $smileID;
            //img
            $img = $this->_doUpload('image-upload');
            if($img == false) $smile->_path = '';
            else{ $smile->_path = $img;
                //unlink old img
                $oldImg = $smile->getSmile($smileID);
                $oldImg = $oldImg['path'];
                unlink($oldImg);
            }
            //update
            $smile->update();
            //delete cache
            $this->db->cache_delete('video', 'view');
            $this->db->cache_delete('video', 'play');

            redirect("admincp/smile/update/$smileID", 'location', 302);
        }
    }

    function _doUpload($postName){
        //do add new
        $config = array();
        $config['upload_path'] = $this->_imageDir;
        $config['allowed_types'] = 'gif|jpg|jpeg|bmp|png'; //file type
        $config['max_size'] = '1024'; //max file size
        $config['encrypt_name'] = 'TRUE'; //encrypt file

        $this->load->library('upload', $config); //load upload library
        $img = $this->upload->do_upload($postName);
        if($img == FALSE) return false;
        else{
            $img = $this->upload->data();
            return $this->_imageDir . $img['raw_name'] . $img['file_ext']; //img path
        }
    }
}
